﻿
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"  %>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.util.Enumeration, java.util.*, java.text.* "%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
function show() {
	//var data = document.f1.registEmail.value;
	//alert(data);
	//location.href="address_list.jsp?email="+data+"&name="+name;
	location.href="address_list.jsp";
}

function registFile() {
	var dataId = document.f2.registId.value;
	var dataName = document.f2.registName.value;
	location.href="address_upload.jsp?id="+dataId+"&name="+dataName;
	
	// location.href="address_upload.jsp";
}

function testFile(val){
		var frm = document.f2;
		
		var obj = document.getElementById("filename");
		obj.select(); //해당 객체 셀렉트
		alert(document.selection.createRange().text.toString());
		obj.blur(); //셀렉트 해제..
		//document.excelLoadForm.uploadFile.select();
		//	alert(document.selection.createRange().text.toString());

	
}


</script>
</head>
<body>

<%
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;
	
	Class.forName("com.mysql.jdbc.Driver"); 		//jdbc driver load
	String url = "jdbc:mysql://localhost:3306/mydb";	//connect db
	String id = "son";
	String password = "0302";

try {
	con = DriverManager.getConnection(url, id, password);
	stmt = con.createStatement(); 			//to make the Statement for SQL

%>

	<%
	String tempId = request.getParameter("loginId");
	String tempPwd = request.getParameter("loginPass");
	
	//request.addArr
		
	//ID or Password 에 null 값이 입력될 경우 (처리 x)
	if (tempId == null || tempPwd == null) {
        //     response.sendRedirect("login.jsp");
    }
    
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT user_name, user_pass, user_id, last_login_time \n");
    sb.append("  FROM mydb.users \n");
    sb.append(" WHERE user_id = '"+ tempId +"' \n");
    sb.append("   AND user_pass = PASSWORD('"+ tempPwd +"')");
    
    System.out.println("query = "+ sb.toString());
    rs = stmt.executeQuery(sb.toString());
		 
	String userId = null;
	String userName = null;
	String lastLoginTime = null;
		
	
	// Login정보 DB와 비교
    while (rs.next()) {  //ID와 Password가 일치할 경우
        userId = rs.getString("user_id");
        userName = rs.getString("user_name");

    }
    
     	//  System.out.println("userId   = "+ userId);
     	//  System.out.println("userName = "+ userName);

    if (userId != null) {        
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String current = dateFormat.format(date);
        System.out.println("current date = "+ current);
        
        sb = new StringBuilder();
        sb.append("UPDATE mydb.users \n");
        sb.append("   SET last_login_time = '"+ current +"' \n");
        sb.append(" WHERE user_id='" + tempId +"'");
        
        System.out.println("lastLoginQuery = "+ sb.toString());
        stmt.executeUpdate(sb.toString());
        
        session.setAttribute("session_id",   userId);
        session.setAttribute("session_name", userName);
        session.setAttribute("session_time", current);

    } else {
        //LOGIN 페이지로 이동
        //  response.sendRedirect("login.jsp");
        //참고
    	//<input type="hidden" name="login" value="<%= 변수명
    }
    %>
    <table>
	    <tr>
	    	<td align="right">User ID   :</td>
	    	<td align="center"> <%=  session.getAttribute("session_id") %></td>
	
	    </tr>
	    <tr>
	    	<td align="right"> User Name :</td>
	    	<td align="center"> <%= session.getAttribute("session_name") %></td>
	    </tr>
		
	    <!--<tr><td><%= session.getAttribute("session_current") %></td><tr>-->
   
	</table>
	<!-- login.jsp 파일로 화면 이동 -->
	
	
	<br>

	<form method = "post" action="address_regist.jsp" name="f1" >
	<table>      
	    <tr>
	    	<td align="center">  ID  </td><td> <input type="text" name="registId" > </td>	    
	    </tr>
	    <tr>
	    	<td align="center">  이름  </td><td> <input type="text" name="registName"> </td>	    
	    </tr> 
	    <tr>
	    	<td align="center">  email  </td><td> <input type="text" name="registEmail"> </td>
	    </tr>
	        
	    <tr></tr>
	    
	    <tr>
	    	<td></td>
	    	<td align="center">
	    		<input type="submit" name="registButton" value="Regist" style = "width :70; height = 25">
	   			<input type="button" name="listButton" value="List" onclick="show()" style = "width :70; height = 25">
	   		</td>
	   	</tr>
	</table> 
	</form>
	
	 <!-- enctype="multipart/form-data" 파일이나 대용량 데이터 보낼때 데이터 전송 방식 --> 

	<form method = "post" enctype="multipart/form-data" action="address_upload.jsp" name="f2" >
	<table>
	    
	    <tr>
	    	<td>
	    		 파일명 : <input type="file" name="filename" id="filename" style = "width :250; height = 25">
	    	</td>
	    </tr>
	    <tr>
	    	<td align="right">
	    		<input type="submit" name="fileUpload" value="File Upload" onclick="testFile(this.value);" style = "width :80; height = 25">
	    	</td>
			<td>
				<input type="submit" name="login" value="Update"  style = "width :70; height = 25">
			</td>
		</tr>
	</table>
	</form>
	
	
	
	<a href="logout.jsp">[LogOut!!!!]</a>
<%
} catch(Exception e) {
    e.printStackTrace();
    throw e;
} finally {
    if (rs   != null)
        try { rs.close(); } catch(Exception e) {};	//JDBC 객체 연결 해제
    if (stmt != null)
        try { stmt.close(); } catch(Exception e) {};
    if (con  != null)
        try { con.close(); } catch(Exception e) {};
}

%>

</body>
</html>